//
//  CNProductViewController.m
//  BeeQuick_19
//
//  Created by 张旭峰 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNProductViewController.h"
#import "CNCarView.h"
#import "CNnameCell.h"
static NSString *cellID = @"cellID";
const CGFloat topViewH = 350;
#define NAVBAR_CHANGE_POINT 10
@interface CNProductViewController ()<UITableViewDelegate,UITableViewDataSourcePrefetching,CarViewDelegate>
@property (weak,nonatomic) UIImageView* topView;
//购物车物品数量
@property (assign,nonatomic) NSInteger count;

@end

@implementation CNProductViewController
{
    CNCarView *_carView;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}
#pragma mark - 搭建界面
- (void)setupUI {
    
    [self.navigationItem setTitle:[NSString stringWithFormat:@"%@",self.model.name]];
    
    UIBarButtonItem *itemB = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"v2_goback"] style:UIBarButtonItemStyleDone target:self action:@selector(backVC)];
    
    self.navigationItem.leftBarButtonItem = itemB;
    
    //设置详情view
    UITableView *detailsView = [[UITableView alloc] init];
    
    [detailsView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    
    detailsView.tableFooterView = [[UIView alloc] init];
    
    detailsView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    //    detailsView.contentInset = UIEdgeInsetsMake(topViewH*0.5, 0, 0, 0);
    detailsView.backgroundColor = [UIColor clearColor];
    detailsView.showsVerticalScrollIndicator = NO;
    
    //设置代理
    detailsView.delegate = self;
    detailsView.dataSource = self;
    [detailsView registerNib:[UINib nibWithNibName:@"CNnameCell" bundle:nil]forCellReuseIdentifier:@"nameID"];
    [self.view addSubview:detailsView];
    
    //设置约束
    [detailsView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(self.view);
        
        make.bottom.equalTo(self.view.mas_bottom).offset(-40);
        
    }];
    
    UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, topViewH)];
    
    //替换字符串
    NSString *string = [_model.img stringByReplacingOccurrencesOfString:_model.pre_img withString:_model.pre_imgs];
    
    NSString *str = @",";
    NSRange rang = [string rangeOfString:str];
    
    //截取从0-rang之间的字符串
    NSString *URLSting = [string substringToIndex:rang.location];
    
    
    [topView sd_setImageWithURL:[NSURL URLWithString:URLSting] placeholderImage:[UIImage imageNamed:@"v2_placeholder_full_size"]];
    
    self.topView = topView;
    topView.contentMode = UIViewContentModeScaleToFill;
    
    detailsView.tableHeaderView = topView;
    
    _carView = [CNCarView bottomCarView];
    
    [self.view addSubview:_carView];
    
    [_carView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.equalTo(self.view);
        
        make.top.equalTo(self.view.mas_bottom).offset(-40);
        
    }];
    _carView.model = _model;
    _carView.count = _model.count;
    
}
-(void)setModel:(CNdetailsModel *)model
{
    _model = model;
    
    //    NSLog(@"pro-----%@",model);
    
}
#pragma mark -CarViewDelegate代理方法
-(void)increaseWithModel:(CNdetailsModel *)model
{
    self.model = model;
    
    //    NSLog(@"increaseWithModel --- %zd",model.count);
    
    
}
-(void)decreaseWithModel:(CNdetailsModel *)model
{
    self.model = model;
        //    NSLog(@"decreaseWithModel --- %zd",model.count);
    
}
#pragma mark -UITableViewDelegate代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 300;
}
#pragma mark - UITableViewDataSource数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CNnameCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nameID" forIndexPath:indexPath];
    
    cell.model = _model;
    
    
    return cell;
    
}

-(void)backVC
{
    
  //  NSDictionary *userInfo = @{
   //                            HMAddFoodKey:_model,
  //                             };
   // [[NSNotificationCenter defaultCenter] postNotificationName:HMAddFoodToCarViewNotification object:self userInfo:userInfo];
    
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 实现navigationBar颜色渐变
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    
    //[self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
   // [self.navigationController.navigationBar lt_reset];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
